﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;

namespace SalesManagement.Controls.List
{
    public partial class MaturityForm : System.Web.UI.UserControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Show()
        {
            this.winDetails.Show();
        }
        public void Show(string maturityId)
        {
            MaturityEntity maturityE = new MaturityEntity();
            MaturityService maturityS = MaturityService.GetInstance();
            maturityE.MaturityId = int.Parse(maturityId);
            maturityE = maturityS.LoadByPrimaryKey(maturityE);
            if (null == maturityE)
                return;
            hdfMaturityId.Value = maturityE.MaturityId;
            txtMaturityName.Text = maturityE.MaturityName;
            txtDay.Text = maturityE.Days.ToString();            
            txtDescription.Text = maturityE.Description;
            ckbIsActive.Checked = maturityE.IsActive;
            this.winDetails.Show();
        }
        public void ShowAndReset()
        {
            this.FormPanel1.Reset();
            this.winDetails.Show();
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            MaturityEntity maturityE = new MaturityEntity();
            MaturityService maturityS = MaturityService.GetInstance();
            if ("" != hdfMaturityId.Value.ToString())
            {
                string maturityId = hdfMaturityId.Value.ToString();
                maturityE.MaturityId = int.Parse(maturityId);
                maturityE = maturityS.LoadByPrimaryKey(maturityE);
                maturityE.MaturityName = txtMaturityName.Text;
                maturityE.Days = int.Parse(txtDay.Text);                
                maturityE.Description = txtDescription.Text;
                maturityE.IsActive = ckbIsActive.Checked;
                maturityE.UpdatedDate = DateTime.Now;
                maturityE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                maturityS.Update(maturityE);
            }
            else
            {
                maturityE.MaturityName = txtMaturityName.Text;
                maturityE.Days = int.Parse(txtDay.Text);     
                maturityE.Description = txtDescription.Text;
                maturityE.IsActive = ckbIsActive.Checked;
                maturityE.CreatedDate = DateTime.Now;
                maturityE.CreatedBy = FSSContext.Current.User.EmployeeId;
                maturityE.UpdatedDate = DateTime.Now;
                maturityE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                maturityS.Insert(maturityE);
            }
            this.winDetails.Hide();
            OnSaveClick();
        }
    }
}